package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.dc.pr.PRError;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private MongoTemplate template;

    @Autowired
    private StrategyRepository repository;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        Strategy strategy =repository.findById(id).orElse(null);
        return strategy;
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //分页查询
        //查询对象
        //1: 查询总数:total
        Query query = new Query();
        long total = template.count(query, Strategy.class);
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //2: 每页显示条数集合: list
        //设置页面显示条数, 还有当前页
        //1 当前页 2 每页的条数 3 排序规则 4 排序字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Strategy> list = template.find(query, Strategy.class);

        //3:封装page对象
        return new PageImpl<Strategy>(list,pageable,total);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        //目的地id 在攻略分类中间接获取
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        // 目的地name
        strategy.setDestName(strategyCatalog.getDestName());
        //主题名称 从攻略主题
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(theme.getName());
        //分类名称
        strategy.setCatalogName(strategyCatalog.getName());
        //是否国外
        List<Destination> toasts = destinationService.findtoasts(strategyCatalog.getDestId());
        if(toasts.size()>0 && toasts !=null){
            Destination destination = toasts.get(0);
            if ("中国".equals(destination.getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        if (StringUtils.hasLength(strategy.getId())){
            //编辑
            if (StringUtils.hasLength(strategy.getId())){
                //查询
                Strategy sty = this.get(strategy.getId());
                //替换
                strategy.setViewnum(sty.getViewnum());
                strategy.setReplynum(sty.getReplynum());
                strategy.setFavornum(sty.getFavornum());
                strategy.setSharenum(sty.getSharenum());
                strategy.setThumbsupnum(sty.getThumbsupnum());
                strategy.setCreateTime(sty.getCreateTime());
            }
            this.update(strategy);
        }else {
            //添加
            //创建时间
            strategy.setCreateTime(new Date());
            this.save(strategy);

        }
    }

    @Override
    public void chenagState(int state, String id) {
        //部分字段更新
        //创建查询对象
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));//where
        Update update = new Update();//$set
        update.set("state",state);//state=1
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByCatalogId(String id) {

        return   repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryviewnumTop3(String destId) {
        //目的地id指定了 点击量倒数 截热门三个
        PageRequest pageRequest = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy> list = repository.findByDestId(destId, pageRequest);
        return   list;
    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);

    }

    @Override
    public List<Strategy> queryByDestName(String keyword) {
        List<Strategy> strategies = repository.queryByDestName(keyword);

        return strategies;
    }
}
